package me.dengfengdecao.android.photogallery;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import android.widget.Toast;

/**
 * 隐藏前台通知的通用fragment
 */
public class VisibleFragment extends Fragment {

    private static final String TAG = "VisibleFragment";

    // 定义广播接收器
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
           // Toast.makeText(getActivity(), "得到一个Broadcast Intent：" + intent.getAction(), Toast.LENGTH_LONG).show();
           //
           setResultCode(Activity.RESULT_CANCELED);
        }
    };

    // 代码中登记receiver
    @Override
    public void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter(PollService.ACTION_SHOW_NOTIFICATION);
        // 传入PERM_PRIVATE，只有我们的应用才能够触发目标receiver
        getActivity().registerReceiver(mBroadcastReceiver, filter, PollService.PERM_PRIVATE, null);
    }

    // 取消receiver
    @Override
    public void onPause() {
        super.onPause();
        getActivity().unregisterReceiver(mBroadcastReceiver);
    }
}
